<template>
	<div class="system-appattr-container standard-container layout-padding layout-pd">
		<div class="review-list-title">
			<div class="title-actions">
				<!-- <el-input v-model="state.search.name" size="default" clearable placeholder="请输入应用属性名称" style="max-width: 180px"> </el-input> -->
				<el-button size="default" type="primary" class="ml10" @click="refresh">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="primary" class="ml10" @click="onOpen('add', '')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增应用属性
				</el-button>
			</div>
		</div>
		<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
			<el-table-column prop="title" label="属性名称" show-overflow-tooltip></el-table-column>
			<el-table-column prop="attrType" label="属性类型" show-overflow-tooltip></el-table-column>
			<el-table-column prop="updateTime" label="修改时间" show-overflow-tooltip></el-table-column>
			<el-table-column prop="updateUsername" label="操作人" show-overflow-tooltip></el-table-column>
			<el-table-column label="操作" width="100">
				<template #default="scope">
					<el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" @click="onOpen('edit', scope.row.id)"
						>修改</el-button
					>
					<el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" @click="onRowDel(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div class="pagination-container">
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				:page-sizes="[10, 15, 20, 30, 50, 100]"
				v-model:current-page="state.tableData.paging.currentPage"
				background
				v-model:page-size="state.tableData.paging.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.paging.totalCount"
			>
			</el-pagination>
		</div>
		<AppAttrDialog ref="AppAttrDialogRef" @refresh="refresh" />
	</div>
</template>

<script setup lang="ts" name="AppAttrInfo">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';

import { AppAttrApi } from '/@/api/shop/appattr';

import msg from '/@/utils/messageUtils';
const AppAttrDialog = defineAsyncComponent(() => import('./dialog.vue'));

const AppAttrDialogRef = ref();
const state = reactive({
	isToggle: false,
	choiceIds: [],
	search: {
		name: '',
	},
	tableData: {
		data: [],
		total: 0,
		loading: false,
		paging: {
			currentPage: 1,
			pageSize: 15,
			totalCount: 0,
			totalPages: 0,
		},
	},
});

const refresh = () => {
	getTableData();
};
const getTableData = async () => {
	try {
		const { data } = await AppAttrApi().search({
			offset: (state.tableData.paging.currentPage - 1) * state.tableData.paging.pageSize,
			limit: state.tableData.paging.pageSize,
		});
		state.tableData.data = data.datas;
		state.tableData.paging.currentPage = data.paging.currentPage;
		state.tableData.paging.totalCount = data.paging.totalCount;
		console.log('result: ', data);
	} catch (e) {
		msg().error('查询失败，请稍后重试');
	}
};
// 分页改变
const onHandleSizeChange = (pageSize: number) => {
	state.tableData.paging.pageSize = pageSize;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (currentPage: number) => {
	state.tableData.paging.currentPage = currentPage;
	getTableData();
};

const onRowDel = (id: string) => {
	console.log('id: ', id);
};

const onOpen = (type: string, id: string) => {
	console.log('type: ', type, ' ,id: ', id);
	AppAttrDialogRef.value.openDialog(type, id);
};
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.system-appattr-container {
	.el-table {
		flex: 1;
	}
}
</style>
